# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         test14_2
# Description:  例14.2
# Author:       ASUS
# Date:         2019/4/17
#-------------------------------------------------------------------------------


import dicttoxml
from xml.dom.minidom import parseString
import os
#定义一个字典
d = [20,'names',
     {'name':'Bill','age':30,'salary':2000},
     {'name':'王军','age':34,'salary':3000},
     {'name':'John','age': 25,'salary': 2500}]
#将字典转换为xml格式
bxml = dicttoxml.dicttoxml(d,custom_root='persons')
#将bytes形式的xml数据按utf-8编码的格式解码成xml字符串
xml = bxml.decode('utf-8')
print(xml)
#解析xml字符串
dom = parseString(xml)
#生成带缩进格式的xml字符串
prettyxml = dom.toprettyxml(indent='   ')
os.makedirs('files',exist_ok = True)
f = open('files/persons.xml','w',encoding='utf-8')
f.write(prettyxml)
f.close()


